package com.kfm.bootdemo.dao;

import com.kfm.bootdemo.model.KfmUser;
import com.kfm.bootdemo.model.Student;
import com.kfm.bootdemo.util.LoadDatasource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 对数据表users的增删改查操作
 */
public class UserDao {
    QueryRunner queryRunner;
    BasicRowProcessor basicRowProcessor;

    public UserDao() {
        try {
            queryRunner = new QueryRunner(LoadDatasource.getDatasource());
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 处理数据行并将其映射到 Java Bean 对象上。
        GenerousBeanProcessor generousBeanProcessor = new GenerousBeanProcessor();
        basicRowProcessor = new BasicRowProcessor(generousBeanProcessor);
    }

    public KfmUser selectByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select *  from `kfm_user` where username = ? and password = ?";
        BeanHandler<KfmUser> kfmUserBeanHandler = new BeanHandler<>(KfmUser.class, basicRowProcessor);
        return queryRunner.query(sql, kfmUserBeanHandler, username, password);
    }

    public int insertUser(String username, String password, String email) throws SQLException {
        String sql = "insert into `kfm_user`(username, password, email) values (?, ?, ?)";
        return queryRunner.update(sql, username, password, email);
    }




}
